import { ChangeSpecs, Product, Monitor, getMonitor, Equipment, ZoneMonitor, DTSource, ProcessAnaInfo, ZoneAnaInfo } from '@/api/datainterface'

const state = {
    spaceId: null,
    homeTime: '',
    monitors: [],
    equipInfo: null,
    zoneMonitors: [],
    dtsources: [],
    processAnaInfos: [],
    zoneAnaInfos: [],
}

const mutations = {
    SET_ID_DATA: (state, data) => {
        state.spaceId = data
    },
    SET_HOME_TIME: (state, data) => {
        state.homeTime = data
    },
    SET_MONITORS_DATA: (state, data) => {
        state.monitors = data
    },
    GET_EQUIPINFO_DATA: (state, data) => {
        state.equipInfo = data
    },
    SET_ZONEMONITOR: (state, data) => {
        state.zoneMonitors = data
    },
    SET_DTSOURCE: (state, data) => {
        state.dtsources = data
    },
    SET_PROCESSANAINFO: (state, data) => {
        state.processAnaInfos = data
    },
    SET_ZONEANAINFO: (state, data) => {
        state.zoneAnaInfos = data
    },
}

const actions = {
    SetSpaceId({ commit }, params) {
        commit('SET_ID_DATA', params)
    },
    SetHomeTime({ commit }, params) {
        commit('SET_HOME_TIME', params)
    },
    ChangeSpecs({ commit }, params) {
        return new Promise((resolve, reject) => {
            ChangeSpecs(params).then(res => {
                const { data } = res
                commit('', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    Product({ commit }, params) {
        return new Promise((resolve, reject) => {
            Product(params).then(res => {
                const { data } = res
                commit('', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    Monitor({ commit }, params) {
        return new Promise((resolve, reject) => {
            Monitor(params).then(res => {
                const { data } = res
                commit('SET_MONITORS_DATA', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    getMonitor({ commit }, params) {
        return new Promise((resolve, reject) => {
            getMonitor(params).then(res => {
                const { data } = res
                const monitors = JSON.parse(data)
                commit('SET_MONITORS_DATA', monitors)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    Equipment({ commit }, params) {
        return new Promise((resolve, reject) => {
            Equipment(params).then(res => {
                const { data } = res
                commit('GET_EQUIPINFO_DATA', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    ZoneMonitors({ commit }, params) {
        return new Promise((resolve, reject) => {
            ZoneMonitor(params).then(res => {
                const { data } = res
                commit('SET_ZONEMONITOR', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    getDTSource({ commit }, params) {
        return new Promise((resolve, reject) => {
            DTSource(params).then(res => {
                const { data } = res
                commit('SET_DTSOURCE', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    ProcessAnaInfo({ commit }, params) {
        return new Promise((resolve, reject) => {
            ProcessAnaInfo(params).then(res => {
                const { data } = res
                commit('SET_PROCESSANAINFO', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    ZoneAnaInfo({ commit }, params) {
        return new Promise((resolve, reject) => {
            ZoneAnaInfo(params).then(res => {
                const { data } = res
                commit('SET_ZONEANAINFO', data)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}